package com.share.tool.generator.code_template.sf.iov;

import com.share.common.constant.DirConstant;
import com.share.common.utils.ObjUtil;
import com.share.tool.generator.code_template.common.BaseConfig;
import com.share.tool.generator.code_template.common.BaseGen;
import com.share.tool.generator.code_template.common.GenConfig;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

public class MainIov extends BaseGen {
    public static void main(String[] args) {
        MainIov instant = new MainIov();
        instant.gen();
    }

    @Override
    protected void init() {

        final String templateLoadingPath = new File(ClassLoader.class.getResource("/template/ftl/sf/iov").getPath()).getPath();


        Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
        cfg.setObjectWrapper(new DefaultObjectWrapper(Configuration.VERSION_2_3_23));
        cfg.setClassicCompatible(true);/*跳过空值*/

        try {
            Map root = new HashMap();
            BaseConfig base = new BaseConfig();
            GenConfig gen = new GenConfig();

            LinkedList<String> list = new LinkedList<>();
            list.add("SunTest");

            cfg.setDirectoryForTemplateLoading(new File(templateLoadingPath));/*预加载目录*/
            for (String modelName : list) {
                File templates[] = new File(templateLoadingPath).listFiles();/*遍历所有模板文件*/
                for (File template : templates) {
                    if (template.isDirectory()) {
                        continue;
                    }
                    File outFile = getFile(gen, template.getName(), modelName);//输出文件定义
                    if (outFile == null) {
                        continue;
                    }

                    System.out.println("正在生产文件:" + outFile.getAbsolutePath());
                    gen.setX(outFile.getName().split("\\.")[0]);

                    Map<String, Object> rootData = ObjUtil.toMap(gen);
                    rootData.putAll(ObjUtil.toMap(base));

                    Template temp = cfg.getTemplate(template.getName());
                    BufferedWriter bw = new BufferedWriter(new FileWriter(outFile));
                    temp.process(rootData, bw);
                    bw.flush();
                    bw.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        return;
    }

    /*对配置文件的命名是有要求的*/
    protected File getFile(GenConfig gen, String template, String modelName) {
        String beanPackage = "";
        if (template.endsWith("Dto.ftl")) {
            beanPackage = gen.getXdPackage();
            String[] split = template.split("-");
            modelName += split[1];
        } else if (template.endsWith("Service.ftl")) {
            beanPackage = gen.getXsPackage();
            modelName += "Service";
        } else if (template.endsWith("ServiceImpl.ftl")) {
            beanPackage = gen.getXsiPackage();
            modelName += "ServiceImpl";
        } else if (template.endsWith("Manager.ftl")) {
            beanPackage = gen.getXmPackage();
            modelName += "Manager";
        } else if (template.endsWith("ManagerImpl.ftl")) {
            beanPackage = gen.getXmiPackage();
            modelName += "ManagerImpl";
        } else if (template.endsWith("Controller.ftl")) {
            beanPackage = gen.getXcPackage();
            modelName += "Controller";
        }

        if (beanPackage == null) {
            beanPackage = "";
        }

        String dirPath = beanPackage.replace(".", "/");
        dirPath = DirConstant.TMP_DIR + File.separator + dirPath;
        File file = new File(dirPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return new File(dirPath + File.separator + modelName + ".java");
    }
}
